/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.logger;

import ca.trader.utils.DebugUtils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Singleton logger for the Trader project
 *
 * @author Ahsan
 */
public class TraderLogger
{

	 private static final Logger logger = Logger.getLogger(TraderLogger.class.getName());
	 private static FileHandler handlerFile;
	 private static LogFormatter formatterTxt;
	 private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	 private static final long LOG_FLUSH_INTERVAL = 120000;
	 private static boolean isActive = false;
	 private static int loggerInstances = 0;

	 // Used to initiate the logger.  This should only be called when starting up the server.
	 public static void init() throws IOException
	 {
		  // Log everything
		  logger.setLevel(Level.ALL);

		  // Set the log handler file.  The name of the handler file is "Trader-[Current Date]-[number of log files created today].log"
		  // Max size of a log is 100 MB.  After the max size is reached, a new log file is created.
		  handlerFile = new FileHandler("%h/Trader-" + getDateNow() + "-%g.log", 104857600, 10000, true);

		  // Create txt Formatter
		  formatterTxt = new LogFormatter();
		  handlerFile.setFormatter(formatterTxt);
		  logger.addHandler(handlerFile);

		  loggerInstances++;

		  isActive = true;
		  new Thread(new Runnable()
		  {
			   @Override
			   public void run()
			   {
				    while (isActive && handlerFile != null)
				    {
						logInfo("TraderLogger", "init", "Flushing logs.  Current instances of loggers = " + loggerInstances);
						handlerFile.flush();
						try
						{
							 Thread.sleep(LOG_FLUSH_INTERVAL);

						}
						catch (Exception ex)
						{
						}
						
				    }
			   }

		  }).start();

	 }

	 /**
	  * User to log a message at level INFO
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message the message
	  */
	 public static void logInfo(String className, String methodName, String message)
	 {
		  logger.logp(Level.INFO, className, methodName, message);
	 }

	 /**
	  * User to log a message at level WARNING
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message the message
	  */
	 public static void logWarning(String className, String methodName, String message)
	 {
		  logger.logp(Level.WARNING, className, methodName, message);
	 }

	 /**
	  * User to log a message at level SEVERE
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message the message
	  * @param throwable the throwable that was thrown (the stack trace is
	  * printed as a string)
	  */
	 public static void logSevere(String className, String methodName, String message, Throwable throwable)
	 {

		  logSevere(className, methodName, message, DebugUtils.getStackTraceAsString(throwable));
	 }

	 /**
	  * User to log a message at level SEVERE
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  * @param message the message
	  * @param stackTrace the stacktrace as a string
	  */
	 public static void logSevere(String className, String methodName, String message, String stackTrace)
	 {
		  logger.logp(Level.SEVERE, className, methodName, message + "\r\n" + stackTrace);
	 }

	 /**
	  * User to log a method entry
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  *
	  */
	 public static void logMethodEntry(String className, String methodName)
	 {
		  logger.entering(className, methodName);
	 }

	 /**
	  * User to log a method exit
	  *
	  * @param className name of the source class
	  * @param methodName name of the source method
	  *
	  */
	 public static void logMethodExit(String className, String methodName)
	 {
		  logger.exiting(className, methodName);
	 }

	 /*
	  * Called when the logger should be closed (to flush the resources).
	  */
	 public static void exiting()
	 {
		  loggerInstances--;
		  isActive = false;
		  handlerFile.close();

	 }

	 public static String getDateNow()
	 {
		  return dateFormat.format(new Date());
	 }

}